package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.ApplicantInfo;
import com.cls.business.service.IApplicantInfoService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.enums.DictEnum;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.system.entity.SysDict;
import com.cls.system.service.ISysDictService;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.BlackList;
import com.cls.business.service.IBlackListService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 黑名单 Controller
 *
 * @author slx
 * @date 2020-11-23 14:56:14
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class BlackListController extends BaseController {

    private final IBlackListService blackListService;
    private final ISysDictService sysDictService;
    private final IApplicantInfoService applicantInfoService;
    /**
    * 黑名单列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/blackList")
    public String blackListIndex(Model model){
        //行政职务
        List<SysDict> positionTypeList = sysDictService.findByParentCode(DictEnum.POSITION.getCode());
        model.addAttribute("positionTypeList",positionTypeList);
        //职称
        List<SysDict> titleTypeList = sysDictService.findByParentCode(DictEnum.TITLE.getCode());
        model.addAttribute("titleTypeList",titleTypeList);
        //证件类型
        List<SysDict> zjlxList = sysDictService.findByParentCode(DictEnum.ZJLX.getCode());
        model.addAttribute("zjlxList",zjlxList);
        return MyUtil.view("business/blackList/blackList");
    }

    /**
    * 黑名单新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/blackList/blackListAdd")
    public String blackListAdd(Model model){
        //证件类型
        List<SysDict> zjlxList = sysDictService.findByParentCode(DictEnum.ZJLX.getCode());
        model.addAttribute("zjlxList",zjlxList);
        return MyUtil.view("business/blackList/blackListAdd");
    }

    /**
    * 黑名单修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/blackList/blackListUpdate/{blackListId}")
    public String blackListUpdate(@PathVariable Long blackListId, Model model){
        BlackList blackList = blackListService.getById(blackListId);
        model.addAttribute("blackList",blackList);
        //证件类型
        List<SysDict> zjlxList = sysDictService.findByParentCode(DictEnum.ZJLX.getCode());
        model.addAttribute("zjlxList",zjlxList);
        return MyUtil.view("business/blackList/blackListUpdate");
    }



    /**
    * 黑名单详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/blackList/blackListDetail/{blackListId}")
    public String blackListDetail(@PathVariable Long blackListId, Model model){
        BlackList blackList = blackListService.getForId(blackListId);
        model.addAttribute("blackList",blackList);
        return MyUtil.view("business/blackList/blackListDetail");
    }

    @GetMapping("blackList")
    @ResponseBody
    @RequiresPermissions("blackList:list")
    public ResponseVO getAllBlackLists(BlackList blackList) {
        return ResponseVO.success(blackListService.findBlackLists(blackList));
    }

    @GetMapping("blackList/list")
    @ResponseBody
    @RequiresPermissions("blackList:list")
    public ResponseVO blackListList(QueryRequest request, BlackList blackList) {
       // PageInfo<BlackList> pageInfo =  this.blackListService.findBlackLists(request, blackList);
      PageInfo<BlackList> pageInfo =  this.blackListService.findBlackListsPage(request, blackList);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增", exceptionMessage = "新增失败")
    @PostMapping("blackList")
    @ResponseBody
    @RequiresPermissions("blackList:add")
    public ResponseVO addBlackList(@Valid BlackList blackList) {
        //查询当前库中是否存在
        BlackList findBlack=new BlackList();
        findBlack.setIdentity(blackList.getIdentity());
        List<BlackList> blackLists=blackListService.findBlackLists(findBlack);
        if(blackLists!=null&&blackLists.size()>0){
            return ResponseVO.failure("新增失败！当前身份证信息已存在此人");
        }
        //根据身份证查询申请人信息
        ApplicantInfo applicantInfo= applicantInfoService.findByIdentity(blackList.getIdentity());
        if(applicantInfo!=null){
            blackList.setApplicantId(applicantInfo.getApplicantId());
        }
        this.blackListService.createBlackList(blackList);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除BlackList", exceptionMessage = "删除失败")
    @PostMapping("blackList/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("blackList:delete")
    public ResponseVO deleteBlackLists(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.blackListService.deleteBlackLists(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个BlackList", exceptionMessage = "删除失败")
    @PostMapping("blackList/delete/{blackListId}")
    @ResponseBody
    @RequiresPermissions("blackList:delete")
    public ResponseVO deleteBlackList(@PathVariable Long blackListId) {
        this.blackListService.removeById(blackListId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改BlackList", exceptionMessage = "修改失败")
    @PostMapping("blackList/update")
    @ResponseBody
    @RequiresPermissions("blackList:update")
    public ResponseVO updateBlackList(BlackList blackList) {

        //根据身份证查询申请人信息
       /* ApplicantInfo applicantInfo= applicantInfoService.findByIdentity(blackList.getIdentity());
        if(applicantInfo!=null){
            blackList.setApplicantId(applicantInfo.getApplicantId());
        }*/
        this.blackListService.updateBlackList(blackList);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改BlackList", exceptionMessage = "导出失败")
    @PostMapping("blackList/excel")
    @ResponseBody
    @RequiresPermissions("blackList:export")
    public void export(QueryRequest queryRequest, BlackList blackList, HttpServletResponse response) {
        List<BlackList> blackLists = this.blackListService.findBlackLists(queryRequest, blackList).getList();
        ExcelKit.$Export(BlackList.class, response).downXlsx(blackLists, false);
    }
}
